package org.mch.func.db.mysql;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.text.BadLocationException;

import org.apache.commons.io.IOUtils;
import org.mch.biz.ui.FuncEvent;

public class MySQLPWEvent extends FuncEvent {
	private static final long serialVersionUID = 420252357552519442L;

	public MySQLPWEvent(MySQLPWPanel panel) {
		super(panel);
	}

	public MySQLPWEvent() {
		super(null);
	}

	public void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_killMysql()) {
			onKillMySQL();
		} else if (e.getSource() == getPanel().getBtn_startMysql()) {
			onStartMySQL();
		} else if (e.getSource() == getPanel().getBtn_modifyPass()) {
			onModifyPass();
		}
	}

	private void onModifyPass() {
		String mysqlPath = getPanel().getText_mysqlPath().getValue();
		String mysqlPass = getPanel().getText_mysqlPass().getValue();
		try {
			// String exec = mysqlPath + "\\bin\\-u root";
			// String exec = "update MySQL.user set password=PASSWORD('" + mysqlPass + "') where
			// User='root'";
			String exec = mysqlPath + "\\bin\\mysqladmin -u root flush-privileges password \"" + mysqlPass + "\"";
			System.out.println(exec);

			Process p = Runtime.getRuntime().exec(exec);
			outLog(IOUtils.toString(p.getErrorStream(), "gbk"));
			outLog(IOUtils.toString(p.getInputStream(), "gbk"));
		} catch (IOException e) {
			e.printStackTrace();
		}

//		try {
//			String exec = "mysql -u root -p"+mysqlPass;
//			String exec = "flush privileges";
//			Process p = Runtime.getRuntime().exec(exec);
//			outLog(IOUtils.toString(p.getErrorStream(), "gbk"));
//			outLog(IOUtils.toString(p.getInputStream(), "gbk"));
//		} catch (IOException e) {
//			e.printStackTrace();
//		}

	}

	private void onStartMySQL() {
		String mysqlPath = getPanel().getText_mysqlPath().getValue();
		try {
			Process p = Runtime.getRuntime().exec(mysqlPath + "\\bin\\mysqld -nt --skip-grant-tables");
			outLog(IOUtils.toString(p.getErrorStream(), "gbk"));
			outLog(IOUtils.toString(p.getInputStream(), "gbk"));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private void onKillMySQL() {
		try {
			Process p = Runtime.getRuntime().exec("net stop mysql");
			outLog(IOUtils.toString(p.getErrorStream(), "gbk"));
			outLog(IOUtils.toString(p.getInputStream(), "gbk"));
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			Process p = Runtime.getRuntime().exec("taskkill /im mysqld.exe /f");
			outLog(IOUtils.toString(p.getErrorStream(), "gbk"));
			outLog(IOUtils.toString(p.getInputStream(), "gbk"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void outLog(String s) {
		try {
			int lineCount = getPanel().getText_log().getLineCount();
			int lineEndOffset = getPanel().getText_log().getLineEndOffset(lineCount - 1);// 确定给定行结尾处的偏移量
			getPanel().getText_log().insert(s, lineEndOffset);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	@Override
	public MySQLPWPanel getPanel() {
		return (MySQLPWPanel) super.getPanel();
	}
}